/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.feed.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.semagia.atomico.dm.IAuthor;

/**
 * Modifiable, not thread-safe implementation of a {@link IPrefixAwareEntity}.
 * <p>
 * An instance must not be modified after it has been handled over to another
 * method.
 * </p>
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 */
abstract class AbstractFeed<T> implements IFeed<T> {

    private final String _id;
    private final String _title;
    private final long _updated;
    protected final List<T> _entries;
    private Set<IAuthor> _authors;
    private String _summary;

    /**
     * Constructs a new instance.
     *
     * @param id The identifier of the feed, not <tt>null</tt>.
     * @param title The title of the feed, not <tt>null</tt>.
     * @param updated Last modification time.
     */
    public AbstractFeed(final String id, final String title, final long updated) {
        this(id, title, updated, null);
    }

    /**
     * Constructs a new instance.
     *
     * @param id The identifier of the feed, not <tt>null</tt>.
     * @param title The title of the feed, not <tt>null</tt>.
     * @param updated Last modification time.
     */
    public AbstractFeed(final String id, final String title, final long updated, final String summary) {
        if (id == null) {
            throw new IllegalArgumentException("The id must not be null");
        }
        if (title == null) {
            throw new IllegalArgumentException("The title must not be null");
        }
        _id = id;
        _title = title;
        _updated = updated;
        _entries = new ArrayList<T>();
        _authors = new HashSet<IAuthor>();
        _summary = summary;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.feed.IFeed#getEntries()
     */
    @Override
    public List<T> getEntries() {
        return Collections.unmodifiableList(_entries);
    }

    /**
     * Adds an entry to this feed.
     *
     * @param entry An entry, never <tt>null</tt>.
     */
    public void addEntry(T entry) {
        if (entry == null) {
            throw new IllegalArgumentException("The entry must not be null");
        }
        _entries.add(entry);
    }

    /**
     * Adds an author to this feed.
     *
     * @param author The author to add.
     */
    public void addAuthor(final IAuthor author) {
        if (author == null) {
            throw new IllegalArgumentException("The author must not be null");
        }
        _authors.add(author);
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IEntity#getAuthors()
     */
    @Override
    public Set<IAuthor> getAuthors() {
        return Collections.unmodifiableSet(_authors);
    }


    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IEntity#getId()
     */
    @Override
    public String getId() {
        return _id;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IEntity#getTitle()
     */
    @Override
    public String getTitle() {
        return _title;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IEntity#getUpdated()
     */
    @Override
    public long getUpdated() {
        return _updated;
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.dm.IEntity#getSummary()
     */
    @Override
    public String getSummary() {
        return _summary;
    }

    /**
     * Sets the summary of this feed.
     *
     * @param summary A plaintext summary or {@code null}.
     */
    public void setSummary(String summary) {
        _summary = summary;
    }

}
